package com.ipet.item.controller.backstage;

import com.github.pagehelper.PageInfo;
import com.ipet.item.pojo.SPU;
import com.ipet.item.service.SPUDetailService;
import com.ipet.item.service.SPUService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/backstage/spu")
@Api("产品模块")
public class SPUController {
    @Autowired
    private SPUService spuService;
    @Autowired
    private SPUDetailService spuDetailService;
    @ApiOperation(value = "对所有的产品进行分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码" , name = "pageNo" , required = false , dataType = "Integer" , paramType = "query"),
            @ApiImplicitParam(value = "每页的数据数" , name = "pageSize" , required = false , dataType = "Integer" , paramType = "query"),
    })
    @GetMapping("/allSpu")
    public ResponseEntity<PageInfo<SPU>> findAllSPU(@RequestParam(defaultValue = "1") Integer pageNo,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<SPU> allSPU = spuService.findAllSPU(pageNo, pageSize);
        if(allSPU == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(allSPU);
    }
    @ApiOperation(value = "对产品进行模糊查询并分页")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "标题关键字" , name = "title" , required = true , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(value = "页码" , name = "pageNo" , required = false , dataType = "Integer" , paramType = "query"),
            @ApiImplicitParam(value = "每页的数据数" , name = "pageSize" , required = false , dataType = "Integer" , paramType = "query"),
    })
    @GetMapping("/spuByTitle")
    public ResponseEntity<PageInfo<SPU>> findSPUByTitle(String title , @RequestParam(defaultValue = "1") Integer pageNo,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<SPU> spuByTitle = spuService.findSPUByTitle(title, pageNo, pageSize);
        if(spuByTitle == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuByTitle);
    }
    @ApiOperation(value = "对产品根据上下架进行查询并分页")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "上架或下架" , name = "saleable" , required = true , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(value = "页码" , name = "pageNo" , required = false , dataType = "Integer" , paramType = "query"),
            @ApiImplicitParam(value = "每页的数据数" , name = "pageSize" , required = false , dataType = "Integer" , paramType = "query"),
    })
    @GetMapping("/spuBySaleable")
    public ResponseEntity<PageInfo<SPU>> findSPUBySaleable(Integer saleable , @RequestParam(defaultValue = "1") Integer pageNo,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<SPU> spuBySaleable = spuService.findSPUBySaleable(saleable, pageNo, pageSize);
        if(spuBySaleable == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuBySaleable);
    }
@ApiOperation(value = "批量上架下架商品")
@ApiImplicitParams({
        @ApiImplicitParam(value = "商品上架或下架" , name = "saleable" , required = true , dataType = "Integer" , paramType = "query"),
        @ApiImplicitParam(value = "上架或下架的商品id集合" , name = "list" , required = true , dataType = "List" , paramType = "body")
})
    @PutMapping("/spuSaleable")
    public ResponseEntity<Integer> updateSaleable(Integer saleable ,@RequestBody List<String> list){
        Integer integer = spuService.updateSaleable(saleable, list);
        if(integer < 1){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(integer);
    }
    @ApiOperation(value = "批量删除商品")
    @ApiImplicitParam(value = "要删除的商品id集合" , name = "list" , required = true , dataType = "List" , paramType = "body")
    @DeleteMapping("/deleteBatch")
    public ResponseEntity<Integer> deleteSpu(@RequestBody List<String> list){
        Integer integer = spuService.deleteSPUBatch(list);
        if(integer < 1){

            return ResponseEntity.notFound().build();
        }
        spuDetailService.deleteBatch(list);
        return ResponseEntity.ok(integer);
    }
    @ApiOperation(value = "添加或修改商品")
    @ApiImplicitParam(value = "要添加或修改的商品" , name = "spu" , required = true , dataType = "SPU"  , paramType = "body")
    @PostMapping("/saveOrUpdate")
    public ResponseEntity<Boolean> insertSPU(@RequestBody SPU spu){
        boolean saveOrUpdate = spuService.saveOrUpdate(spu);
        return ResponseEntity.ok(saveOrUpdate);
    }

}
